package com.hmdp.service.impl;
import java.security.Key;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hmdp.dto.Result;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.Follow;
import com.hmdp.entity.User;
import com.hmdp.entity.UserInfo;
import com.hmdp.mapper.FollowMapper;
import com.hmdp.service.IFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.service.IUserInfoService;
import com.hmdp.service.IUserService;
import com.hmdp.utils.RedisConstants;
import com.hmdp.utils.UserHolder;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
@RequiredArgsConstructor
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {

    private  final StringRedisTemplate stringRedisTemplate;
    private  final IUserService service;


    @Override
    public Result follow(Long id, Boolean isFollow) {
        //1.获取到用户id
        Long userId = UserHolder.getUser().getId();
        String key= "follows:"+userId;
        if (isFollow) {
            //2.关注,新增
            Follow follow = new Follow();
            follow.setUserId(userId);
            follow.setFollowUserId(id);
            boolean isSuccess = save(follow);
            if (isSuccess) {
                //成功存入redis
            stringRedisTemplate.opsForSet().add(key,id.toString());
            }
        }else {
            //3.取关,删除
            boolean isSuccess =remove(new QueryWrapper<Follow>().eq("user_id",userId).eq("follow_user_id",id));
            if (isSuccess) {
                stringRedisTemplate.opsForSet().remove(key,id.toString());
            }
        }

        return Result.ok();
    }

    @Override
    public Result isFollow(Long id) {
        Long userId = UserHolder.getUser().getId();
        //查询是否存在
        Integer count = query().eq("follow_user_id", id).eq("user_id",userId).count();
        return Result.ok(count>0);
    }

    @Override
    public Result followCommn(Long id) {
        Long userId = UserHolder.getUser().getId();
        //当前登录用户id
        String key= "follows:"+userId;
        //点入主业的用户id
        String key2= "follows:"+id;
        //查询reid中的交集然后返回
        Set<String> intersect = stringRedisTemplate.opsForSet().intersect(key, key2);
        if (intersect==null || intersect.isEmpty()) {
            return Result.ok(Collections.emptyList());
        }
        List<Long> collect = intersect.stream().map(Long::valueOf).toList();

        List<User> userList = service.listByIds(collect);
        List<UserDTO> userDTOList = BeanUtil.copyToList(userList, UserDTO.class);
        return Result.ok(userDTOList);
    }
}
